package com.m2h.xiaoke.api;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.iflytek.cloud.Setting;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.m2h.xiaoke.param.Constants;

/**
 * @author lijh
 */
public class XunfeiVoice {
    private Context context;
    private static XunfeiVoice xunfeiVoiceInstance = null;

    private XunfeiVoice() {

    }
    public static XunfeiVoice getInstance() {
        if (xunfeiVoiceInstance == null) {
            synchronized (XunfeiVoice.class) {
                if (xunfeiVoiceInstance == null) {
                    xunfeiVoiceInstance = new XunfeiVoice();
                }
            }
        }
        return xunfeiVoiceInstance;
    }

    /**
     * 初始化整个语音模块
     *
     * @param context
     */
    public void initVoice(Context context) {
        this.context = context;
        LogUtils.d("initVoice");
        initializeIFly();
        XunfeiSpeak.getInstance().initSpeaker(context);
        XunfeiUnderstand.getInstance().init(context);
        XunfeiAwaken.getInstance().init(context);
    }

    /**
     * 反初始化
     */
    public void uninitVoice() {
        LogUtils.d("uninitVoice");
        XunfeiAwaken.getInstance().uninit();
        XunfeiUnderstand.getInstance().uninit();
        XunfeiSpeak.getInstance().uninitSpeaker();
    }

    private void initializeIFly() {
        Setting.setShowLog(true);
        StringBuffer param = new StringBuffer();
        param.append("appid=" + Constants.APP_ID);
        param.append(",");
        // 设置使用v5+
        param.append(SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC);
        SpeechUtility.createUtility(context, param.toString());
    }
}
